//index.js
//获取应用实例
const app = getApp();
var rate = 0;
var canvasWidth = 0;
var canvasHeight = 0;
const md = require('../../utils/md5.js');
const post = require('../../utils/post.js');
Page({
  data: {
    imgUrls: [
      'http://eplus.zokoo.com.cn/upload/wallet/lunbo1.jpg'
    ],
    citys: '厦门市',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    customItem: '全部',
    lineCanvasData: {
      canvasId: 'lineAreaCanvas',
    },
    model: 1,
    userinfo: wx.getStorageSync('userinfo'),
    serviceList: [],
    shopinfo: null
  },
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let accid = options.id;
    let toget = options.toget ? options.toget : 0;
    this.setData({
      accid: accid,
      toget: toget
    })
    var systemInfo = app.systemInfo;
    rate = systemInfo.screenWidth / 750;
    var updateData = {};
    canvasWidth = systemInfo.screenWidth - rate * 64 - 20;
    canvasHeight = rate * 306 + rate * 44 + rate * 34 + rate * 22;

    var yMax = 500;
    var yMin = 0;
    var xMax = 30;
    var xMin = 0;
    updateData['lineCanvasData.canvasWidth'] = canvasWidth;
    updateData['lineCanvasData.notiao'] = 1;
    updateData['lineCanvasData.axisPadd'] = {
      left: 0,
      top: -10,
      right: rate * 5
    };
    updateData['lineCanvasData.axisMargin'] = {
      bottom: rate * 34,
      left: rate * 26
    };
    updateData['lineCanvasData.yAxis.fontSize'] = rate * 22;
    updateData['lineCanvasData.yAxis.fontColor'] = '#637280';
    updateData['lineCanvasData.yAxis.lineDash'] = {
      pattern: [0, 0],
      offset: 0
    }
    updateData['lineCanvasData.yAxis.lineColor'] = '#F2F2F2';
    updateData['lineCanvasData.yAxis.lineWidth'] = rate * 2;
    updateData['lineCanvasData.yAxis.dataWidth'] = rate * 62;
    updateData['lineCanvasData.yAxis.isShow'] = true;
    updateData['lineCanvasData.yAxis.isDash'] = true;
    updateData['lineCanvasData.yAxis.minData'] = yMin;
    updateData['lineCanvasData.yAxis.maxData'] = yMax;
    updateData['lineCanvasData.yAxis.padd'] = rate * 306 / (yMax - yMin);

    updateData['lineCanvasData.xAxis.dataHeight'] = rate * 26;
    updateData['lineCanvasData.xAxis.fontSize'] = rate * 22;
    updateData['lineCanvasData.xAxis.fontColor'] = '#637280';
    updateData['lineCanvasData.xAxis.lineColor'] = '#DCE0E6';
    updateData['lineCanvasData.xAxis.lineWidth'] = rate * 2;
    updateData['lineCanvasData.xAxis.minData'] = xMin;
    updateData['lineCanvasData.xAxis.maxData'] = xMax;
    updateData['lineCanvasData.xAxis.padd'] = (canvasWidth - rate * 103) / (xMax - xMin);

    updateData['lineCanvasData.point'] = {
      size: rate * 4,
      isShow: false
    };
    updateData['lineCanvasData.canvasHeight'] = canvasHeight - 20;

    // console.log(updateData)
    this.setData(updateData);
    // this.showzhexian(accid);
    this.showzhexian(accid);
    this.getshopinfo(accid);

  },
  getserviceHistory: function(id) {
    var that = this;
    var data = {};
    data.where = "DepID = " + id;
    var url = "/BO/PE_ActiveHistory/Query";
    var callback = function(res) {
      console.log(res);
      if (res.data.code == 0 && res.data.data.length > 0) {
        let datas = res.data.data;
        for (let k in datas) {
          datas[k].times = datas[k].ActiveDate.split(' ')[0]
        }
        that.setData({
          serviceList: datas
        })
      }
    };
    var res = post.getdata(url, data, callback);
  },
  onShow: function() {
    this.getserviceHistory(this.data.accid);
  },
  getshopinfo: function(id) {
    var that = this;
    var data = {};
    data.where = "AccID = " + id;
    var url = "/BO/TR_Account/Query";
    var callback = function(res) {
      console.log(res);
      if (res.data.code == 0 && res.data.data.length > 0) {
        that.setData({
          shopinfo: res.data.data[0]
        })
      }
    };
    var res = post.getdata(url, data, callback);
  },
  showzhexian: function(depid) {
    var that = this;
    // 获取返利详情
    var datas = {};
    datas.AccID = depid;
    datas.Today = '2018-10-29';
    datas.pageindex = 1;
    datas.pagesize = 31;
    datas.summary = 1;
    var datacall = function(res) {
      // console.log(res);
      if (res.data.data.length != 0) {
        var daymoney = res.data.data;
        var moneymin = daymoney[0].SASumCount; //最小的营收
        var moneymax = daymoney[0].SASumCount; //最大的营收
        var ordertotal = 0;
        for (var i = 0; i < daymoney.length; i++) {
          if (moneymax < daymoney[i].SASumCount) {
            moneymax = daymoney[i].SASumCount;
          }
          if (moneymin > daymoney[i].SASumCount) {
            moneymin = daymoney[i].SASumCount;
          }
          ordertotal += daymoney[i].SASumCount;
        }
        that.setData({
          mmx: moneymax,
          ordertotal: ordertotal
        })
        console.log('最大为：' + moneymax + '最小为：' + moneymin)
        var systemInfo = app.systemInfo;
        rate = systemInfo.screenWidth / 750;
        var updateData = {};
        canvasWidth = systemInfo.screenWidth - rate * 64 - 25;
        canvasHeight = 100; //rate * 306 + rate * 44 + rate * 34 + rate * 22;
        var yMax = 500;
        var yMin = 0;
        var xMax = 30;
        // var xMin = 6500;
        var xMin = moneymin;
        if (xMin <= 500) {
          xMin = 0;
        } else {
          xMin = Math.floor(xMin / 100) * 100 - Math.floor((Math.ceil((moneymax - moneymin) / 10)) / 100) * 100;
          if (xMin < 0) {
            xMin = 0;
          }
        }
        var kedu = Math.ceil((moneymax - moneymin) / 400);
        if (kedu <= 1) {
          kedu = 100;
        } else {
          kedu = kedu * 100;
        }
        console.log(kedu);
        var dayx = [];
        var dayy = [];
        dayy[0] = {
          x: -1,
          y: 0,
          title: '日营收'
        };
        var iii = {};
        var bbb = {};
        for (var i = 0; i < daymoney.length; i++) {
          if (i == 6) {
            iii.title = daymoney[i].SASumCount;
            iii.isShowTitle = true;
          }
          iii.x = i;
          iii.y = (daymoney[i].SASumCount - xMin) / (kedu / 100); // * 1.8;
          dayx[i] = iii;
          bbb.x = i;
          bbb.y = 0;
          bbb.title = daymoney[i].AccDate.split(' ')[0];
          bbb.title = bbb.title.split('-')[1] + "." + bbb.title.split('-')[2];
          dayy[i + 1] = bbb;
          iii = {};
          bbb = {};
        }
        console.log(dayx); //组装好的y上的数据
        console.log(dayy); //组装好的x上的数据
        var series = [{
          // data: [
          //   { x: 0, y: (7400 - xMin) * 1.8 },
          //   { x: 1, y: (7000 - xMin) * 1.8 },
          //   { x: 2, y: (8000 - xMin) * 1.8 },
          //   { x: 3, y: (7590 - xMin) * 1.8 },
          //   { x: 4, y: (9500 - xMin) * 1.8 },
          //   { x: 5, y: (8500 - xMin) * 1.8 },
          //   { x: 6, y: (10800 - xMin) * 1.8, title: '￥10800', isShowTitle: true }
          // ]
          data: dayx
        }];
        var xAxisData = dayy;
        // var xAxisData = [
        //   { x: -1, y: 0, title: '日营收' },
        //   { x: 0, y: 0, title: '4.15' },
        //   { x: 1, y: 0, title: '4.16' },
        //   { x: 2, y: 0, title: '4.17' },
        //   { x: 3, y: 0, title: '4.18' },
        //   { x: 4, y: 0, title: '4.19' },
        //   { x: 5, y: 0, title: '4.20' },
        //   { x: 6, y: 0, title: '4.21' }
        // ];
        // console.log(xAxisData)
        var ysize = 100;
        var yAxisData = [{
            x: 0,
            y: 0,
            title: xMin
          },
          {
            x: 0,
            y: 1 * ysize,
            title: xMin + kedu
          },
          {
            x: 0,
            y: 2 * ysize,
            title: xMin + 2 * kedu
          },
          {
            x: 0,
            y: 3 * ysize,
            title: xMin + 3 * kedu
          },
          {
            x: 0,
            y: 4 * ysize,
            title: xMin + 4 * kedu
          },
          // { x: 0, y: 5 * ysize, title: xMin + 5 * kedu },
        ];
        yMax = 500;
        yMin = 0;
        xMax = 6;
        xMin = 0;
        updateData['lineCanvasData.xAxis.minData'] = xMin;
        updateData['lineCanvasData.xAxis.maxData'] = xMax;
        updateData['lineCanvasData.xAxis.padd'] = (canvasWidth - rate * 98) / (xMax - xMin);
        updateData['lineCanvasData.point'] = {
          size: rate * 4,
          isShow: false
        };
        updateData['lineCanvasData.yAxis.minData'] = yMin;
        updateData['lineCanvasData.yAxis.maxData'] = yMax;
        updateData['lineCanvasData.yAxis.padd'] = rate * 306 / (yMax - yMin);
        updateData['lineCanvasData.series'] = series;
        updateData['lineCanvasData.xAxis.data'] = xAxisData;
        updateData['lineCanvasData.yAxis.data'] = yAxisData;
        that.setData(updateData);
      }
    }
    //var res2 = post.getdata("/Action/PK01/GetSale7DaySumRpt", datas, datacall); //Action / PK01 / GetTran7DaySumRpt
    var res2 = post.getdata("/Action/PK01/GetTran7DaySumRpt", datas, datacall); //Action / PK01 / GetTran7DaySumRpt

  },
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    console.log("22222");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  togetshop: function() {
    var that = this;
    wx.showModal({
      title: '',
      content: '是否确定领取商户？',
      success: function(res) {
        if (res.confirm) {
          var data = {};
          data.AccID = that.data.accid;
          data.ServiceMebID = wx.getStorageSync('userinfo').MebID;
          var url = "/Action/PK01/SetServiceMebID";
          var callback = function(res) {
            console.log(res);
            wx.navigateBack({
              delta: 1
            })
            return;
            if (res.data.code == 0) {
              wx.showModal({
                title: '',
                content: '商户认领成功！',
                success: function(res) {
                  if (res.confirm) {
                    wx.navigateBack({
                      delta: 1
                    })
                  } else {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                },
              })
            }
          };
          var res = post.getdata(url, data, callback);
        } else {
          return;
        }
      }
    })

  },
  tocall: function() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shopinfo.Tel,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  showimages: function(e) {
    let url = e.currentTarget.dataset.imgurl;
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  toreceive: function() {
    wx.navigateTo({
      url: '/pages/receivables/receivables',
    })
  },
  torecord1: function(e) {
    wx.navigateTo({
      url: '/pages/record/record?etype=1&accid=' + this.data.accid,
    })
  },
  torecord2: function(e) {
    wx.navigateTo({
      url: '/pages/record/record?etype=2&accid=' + this.data.accid,
    })
  },
  torecord3: function(e) {
    wx.navigateTo({
      url: '/pages/record/record?etype=3&accid=' + this.data.accid,
    })
  },
})